//template<class T> inline Print &operator <<(Print &obj, T arg) {
//  obj.print(arg);
//  return obj;
//}


#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

#include <Wire.h>
#include <Adafruit_BMP085.h>


#include "DHT.h"

#define DHTPIN 13     // what digital pin we're connected to
#define DHTTYPE DHT11   // DHT 11

Adafruit_BMP085 bmp;

DHT dht(DHTPIN, DHTTYPE);

// U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
// U8G2_SSD1306_128X64_NONAME_1_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
U8G2_SSD1306_128X64_NONAME_1_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 6, /* data=*/ 7, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);


char line1[64];
char line2[64];
char line3[64];
char line4[64];
char line5[64];

void setup(void) {
  Serial.begin(9600);
  bmp.begin();
  Serial.println("Hello DHT!");
  u8g2.begin();
  dht.begin();
}

void loop(void) {
  u8g2.firstPage();
  do {
    // u8g2.setFont(u8g2_font_10x20_tf);
    // u8g2.setFont(u8g2_font_7x13_tf);
    u8g2.setFont(u8g2_font_6x12_tf);
    u8g2.drawStr(0, 12, line1);
    u8g2.drawStr(0, 24, line2);
    u8g2.drawStr(0, 36, line3);
    u8g2.drawStr(0, 48, line4);
    u8g2.drawStr(0, 60, line5);
  } while ( u8g2.nextPage() );

  // Wait a few seconds between measurements.
  delay(1000);

  Serial.print("Temperature = ");
  float temperature = bmp.readTemperature();
  char temperature_str[16];
  dtostrf(temperature, 2, 1, temperature_str);
  Serial.print(temperature);
  Serial.println(" *C");

  sprintf(line1, "Temperature    %s'C", temperature_str);

  Serial.print("Pressure = ");
  float pressure = bmp.readPressure();
  char pressure_str[16];
  dtostrf(pressure / 1000.0, 2, 3, pressure_str);
  Serial.print(pressure);
  Serial.println(" Pa");

  // Calculate altitude assuming 'standard' barometric
  // pressure of 1013.25 millibar = 101325 Pascal
  Serial.print("Calculated altitude = ");
  float altitude = bmp.readAltitude();
  char altitude_str[16];
  dtostrf(altitude, 2, 1, altitude_str);
  Serial.print(altitude);
  Serial.println(" meters");
  Serial.println();
  sprintf(line4, "Pressure   %skPa", pressure_str);
  sprintf(line5, "Ref altitude   %sm", altitude_str);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
  } else {
    // Compute heat index in Fahrenheit (the default)
    float hif = dht.computeHeatIndex(f, h);
    // Compute heat index in Celsius (isFahreheit = false)
    float hic = dht.computeHeatIndex(t, h, false);

    Serial.print("Relative humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.print(" *C ");
    Serial.print(f);
    Serial.print(" *F\t");
    Serial.print("Heat index: ");
    Serial.print(hic);
    Serial.print(" *C ");
    Serial.print(hif);
    Serial.println(" *F");

    float body_temperature = dht.computeHeatIndex(temperature, h, false);

    // char temp_str[16];
    // dtostrf(t, 2, 1, temp_str);
    char humidity_str[16];
    dtostrf(h, 2, 1, humidity_str);
    char body_temp_str[16];
    dtostrf(body_temperature, 2, 1, body_temp_str);

    sprintf(line2, "Humidity       %s %%", humidity_str);
    sprintf(line3, "Body feeling   %s'C", body_temp_str);
  }
}

